const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const util = require('../../utils/util.js');
Page({
  data: {
    type:1,
    busnums:1,
    drivenums:1,
    quesnums:1,
    cur_month: 0,
    cur_year: 0,
    weeklist: ['日', '一', '二', '三', '四', '五', '六'],
    bottomLift: 0,  
    detail:'',
    ticket:[],
    timelist:[],
    id:'',
    daylists:[],
    daylists1:[],
    activityTimeId:'',
    time:''
  },
  onLoad: function (option) {
    console.log(app.globalData.parentid)
    let {bottomLift} = app.globalData
    if(bottomLift>=0){
      app.getDeviceSize().then(res => {
        const {bottomLift} = res
        this.setData({
          bottomLift
        })
      })
    }else{
      this.setData({
        bottomLift
      })
    }
    if(option.id){
      this.setData({
        id:option.id
      })
      this.activitydetail()

    }
  },
  activitydetail(){
    Controller.activitydetail({activityId:this.data.id}).then(res=>{
      if(res.data.baseData){
        this.setData({
          detail:res.data.baseData,
          activityTimeId1:res.data.baseData.activityTimeId
        })
        var cur_year = new Date().getFullYear();   //获取当前年
        var cur_month = new Date().getMonth();    //获取当前月
        this.setData({
          cur_year: cur_year,
          cur_month: cur_month + 1
        });
        this.calendar(cur_year, cur_month + 1);
        this.ticketList()
      }
    })
  },
  ticketList(){
    Controller.ticketList({activityId:this.data.id,activityTimeId:this.data.activityTimeId1}).then(res=>{
      if(res.data.baseData){
        for(var item of res.data.baseData){
          item.nums=0;
        }
        this.setData({
          ticket:res.data.baseData
        })
        
      }
    })
  },
  changetype(e){
    this.setData({
      type:e.currentTarget.dataset.type
    })
  },
  mins(e){
    var index=e.currentTarget.dataset.index;
    var lists=this.data.ticket;
    var number=0;
    for(var i=0;i<lists.length;i++){
      if(i==index){
        lists[i].nums--;
        if(lists[i].nums<=0){
          lists[i].nums=0
        }
      }
    }
    
    this.setData({
      ticket:lists
    })
  },
  plus(e){
    var index=e.currentTarget.dataset.index;
    var lists=this.data.ticket;
    var stock=0;
    var number=0;
    for(var i=0;i<lists.length;i++){
      if(i==index){
        lists[i].nums++;
        if(lists[i].nums>=lists[i].stock){
          lists[i].nums=lists[i].stock;
        }
      }
    }
   this.setData({
      ticket:lists
    })
  },
  changenums(e){
    var index=e.currentTarget.dataset.index;
    var lists=this.data.ticket;
    var stock=0;
    var number=0;
    for(var i=0;i<lists.length;i++){
      if(i==index){
        lists[i].nums=e.detail.value
        if(lists[i].nums>=lists[i].stock){
          lists[i].nums=lists[i].stock;
        }
        if(lists[i].nums<=0){
          lists[i].nums=0
        }
      }
    }
    
    this.setData({
      ticket:lists
    })
  },
  buy(){
    var ticket=this.data.ticket;
    var lists=[];
    var lists1=[];
    for(var i=0;i<ticket.length;i++){
      if(ticket[i].nums>0){
        lists.push({ticketId:ticket[i].id,count:ticket[i].nums});
        lists1.push(ticket[i]);
      }
    }
    
    if(lists<=0){
      wx.showToast({
        icon:'none',
        title:"请选择票类!"
      })
      return
    }
    if(!this.data.activityTimeId){
      wx.showToast({
        icon:'none',
        title:"请选择日期!"
      })
      return
    }
    
    var data={
      activityId:this.data.id,
      activityTimeId:this.data.activityTimeId,
      ticketBody:JSON.stringify(lists),
      parentId:app.globalData.parentid || ''
    }
    Controller.ordercreate(data).then(res=>{
      if(res.data.baseData){
        wx.navigateTo({
          url:"/pages/detail/info?no="+res.data.baseData+"&price="+res.data.preAmount+"&id="+this.data.id+"&time="+this.data.time
        });
        app.globalData.ticket=lists1
      }
    })
    
  },
  calendar: function(year, month) {
    var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

    /**
     * 本月天数
     */
    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
      days[1] = 29;
      }

    var day = days[month - 1];

    /**
     * 本月第一天星期几
     */
    var myDate = new Date(year, month - 1, 1);
    var weektime = myDate.getDay();

    /**
     * 上个月的天数
     */
    if(month == 1) {
      var prevyear = year - 1;
      var prevmonth = 12;
      var prevday = 31;
    } else {
      prevmonth = month - 1;
      prevday = days[prevmonth - 1];
    }

    /**
     * 下个月的天数
     */
    if(month == 12) {
      var nextyear = year + 1;
      var nextmonth = 1;
      var nextday = 31;
    } else {
      nextmonth = month + 1;
      nextday = days[nextmonth - 1];
    }

    /**
     * 日历上第一个数字
     */
    if(weektime == 0) {
      var firstnumber = 1;
    } else {
      firstnumber = prevday - weektime + 1;
    }

    /**
     * 日历上最后一个数字
     */
    if(firstnumber == 1) {
      var lastnumber = 7 - (day % 7);
    } else {
      if ((prevday - firstnumber + 1 + day) % 7 == 0) {
        lastnumber = day;
      } else {
        var remainder = (prevday - firstnumber + 1 + day) % 7;
        lastnumber = 7 - remainder;
      }
    }

    var lastMonthDaysList = [];
    var currentMonthDaysList = [];
    var nextMonthDaysList = [];

    if(firstnumber == 1) {
      lastMonthDaysList = [];
    } else{
      for(var i = firstnumber; i <= prevday; i++) {
        lastMonthDaysList.push(i);
      }
    }

    for(var i = 1; i <= day; i++) {
      currentMonthDaysList.push(i);
    }

    if(lastnumber == day) {
      nextMonthDaysList = [];
    } else {
      for(var i = 1; i <= lastnumber; i++) {
        nextMonthDaysList.push(i);
      }
    }
    var lists=[];
    for(var i=0;i<currentMonthDaysList.length;i++){
      var item1=currentMonthDaysList[i];
      lists.push({day:item1});
    }
    this.setData({
      lastMonthDaysList: lastMonthDaysList,
      currentMonthDaysList: currentMonthDaysList,
      nextMonthDaysList: nextMonthDaysList,
      daylists:lists,
    })
    this.acvitityTimeList()
  },
  acvitityTimeList(){
    var month=this.data.cur_month<10?'0'+this.data.cur_month:this.data.cur_month
    Controller.acvitityTimeList({activityId:this.data.id,month:this.data.cur_year+'-'+month}).then(res=>{
      if(res.data.baseData){
        for(var item of res.data.baseData){
          item.nums=0;
        }
        this.setData({
          timelist:res.data.baseData
        })
        var daylists=this.data.daylists;
        var timelist=this.data.timelist;
        for(var i=0;i<daylists.length;i++){
          var item1=daylists[i];
          for(var j=0;j<timelist.length;j++){
            var item2=timelist[j];
            if(item1.day==Number(item2.day)){
              daylists[i]=item2;
              daylists[i].select=false;
              break
            }
          }
        }
        this.setData({
          daylists,
          activityTimeId:'',
        })
      }
    })
  },
  select(e){
    var id=e.currentTarget.dataset.id;
    var daylists=this.data.daylists;
    var time=e.currentTarget.dataset.time;
    for(var i=0;i<daylists.length;i++){
      daylists[i].select=false;
      if(id==daylists[i].id){
        daylists[i].select=true;
      }
    }
    this.setData({
      daylists,
      activityTimeId:id,
      activityTimeId1:id,
      time,
    })
    this.ticketList()
  },
  prevMonth: function(e) {
    var cur_year = this.data.cur_year;

    if(this.data.cur_month == 1) {
      var cur_month = 12;
      cur_year = cur_year - 1;
    } else {
      cur_month = this.data.cur_month - 1;
    }
    
    this.setData({
      cur_year: cur_year,
      cur_month: cur_month
    });
    this.calendar(cur_year, cur_month);
  },
  nextMonth: function(e) {
    var cur_year = this.data.cur_year;

    if(this.data.cur_month == 12) {
      var cur_month = 1;
      cur_year = cur_year +1;
    } else {
      cur_month = this.data.cur_month +1;
    }
    
    this.setData({
      cur_year: cur_year,
      cur_month: cur_month
    });
    this.calendar(cur_year, cur_month);
  },

  changeYear: function(e) {
    var cur_year = this.data.cur_year - 1;
    var cur_month = this.data.cur_month;

    this.setData({
      cur_year: cur_year,
      cur_month: cur_month
    })

    this.calendar(cur_year, cur_month);
   
  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  },
  onShareAppMessage: function () {

  },

})